/*
 * Copyright 2009 IT Mill Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.tutos.ui;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;

import com.tutos.ui.login.LoginScreen;
import com.vaadin.Application;
import com.vaadin.service.ApplicationContext;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

/**
 * The Application's "main" class
 */
public class TutosApplication extends Application implements ApplicationContext.TransactionListener
{
	private static final long serialVersionUID = 1L;
	private static ThreadLocal<TutosApplication> currentApplication = new ThreadLocal<TutosApplication>();
	private Window mainWindow;
	private VerticalLayout background;
	
	public Window getMainWindow()
	{
		return mainWindow;
	}


	@Override
	public void init()
	{
		
		
		//madeSubject();
        
		this.getContext().addTransactionListener(this);
		
				
		setTheme("TutosTheme");
		this.mainWindow = new Window("Tutos");
		mainWindow.getContent().setSizeFull();

		this.setMainWindow(mainWindow);

		mainWindow.addComponent(new LoginScreen(this));

	}
	
	public void login(String username, String password)
	{
		UsernamePasswordToken token;

		token = new UsernamePasswordToken(username, password);
		// ”Remember Me” built-in, just do this:
		token.setRememberMe(true);

		// With most of Shiro, you'll always want to make sure you're working with the currently executing user,
		// referred to as the subject
		Subject currentUser = SecurityUtils.getSubject();
		currentUser.getSession().setAttribute("username", username);
		
		// Authenticate
		currentUser.login(token);
	}


	public void logout()
	{
		getMainWindow().getApplication().close();

		Subject currentUser = SecurityUtils.getSubject();

		if (currentUser.isAuthenticated())
		{
			currentUser.logout();
		}
	}


	public static TutosApplication getInstance()
	{
		return TutosApplication.currentApplication.get();
	}


	@Override
	public void transactionStart(Application application, Object transactionData)
	{
		if (application == TutosApplication.this)
		{
			TutosApplication.currentApplication.set(this);
		}
	}


	@Override
	public void transactionEnd(Application application, Object transactionData)
	{
		if (application == TutosApplication.this)
		{
			TutosApplication.currentApplication.set(null);

			TutosApplication.currentApplication.remove();
		}
	}

	
	public static class LogoutListener implements Button.ClickListener
	{
		private static final long serialVersionUID = 1L;
		private TutosApplication app;


		public LogoutListener(TutosApplication app)
		{
			this.app = app;
		}


		@Override
		public void buttonClick(ClickEvent event)
		{
			this.app.logout();
		}
	}
	
}
